package com.pakazaka.simplecrawler;

import com.pakazaka.json.ParseJSON;
import static com.pakazaka.json.ParseJSON.parseJSONArray;
import com.pakazaka.player.Player;
import com.pakazaka.testItemsSystem.Item;
import com.pakazaka.testItemsSystem.ItemPrototype;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author Riseremi
 */
public class PrototypeLoader {

    private static ArrayList<HashMap<String, String>> hmArray = new ArrayList<>();
    //
    private static HashMap<String, String> names = new HashMap<>();
    private static HashMap<String, String> descriptions = new HashMap<>();
    private static HashMap<String, String> maxDurabilities = new HashMap<>();
    private static HashMap<String, String> weights = new HashMap<>();
    private static HashMap<String, String> volumes = new HashMap<>();
    private static HashMap<String, String> powers = new HashMap<>();
    private static HashMap<String, String> noises = new HashMap<>();
    private static HashMap<String, Item.Type> types = new HashMap<>();
    private static HashMap<String, String> imageIds = new HashMap<>();
    private static HashMap<String, String> actions = new HashMap<>();
    private static HashMap<String, String> capacities = new HashMap<>();
    private static HashMap<String, String> afterUse = new HashMap<>();
    private static HashMap<String, String> cutProtection = new HashMap<>();
    private static HashMap<String, String> bashProtection = new HashMap<>();
    private static HashMap<String, String> pierceProtection = new HashMap<>();
    private static HashMap<String, String> comfort = new HashMap<>();
    private static HashMap<String, Player.EffectType[]> effect = new HashMap<>();
    //arrays
    private static HashMap<String, String> properties = new HashMap<>();
    private static HashMap<String, String> materials = new HashMap<>();
    private static HashMap<String, Item.Slot> slot = new HashMap<>();

    private static void fillHashMap(String field, HashMap<String, String> map) {
        for (HashMap<String, String> temp : hmArray) {
            String id = temp.get("id");
            map.put(id, temp.get(field));
        }
    }

    //method loads all effects in array HELL YEAH
    private static void fillEffectHashMap(String field, HashMap<String, Player.EffectType[]> map) {
        for (HashMap<String, String> temp : hmArray) {
            String id = temp.get("id");
            ArrayList<Player.EffectType> effectsList = new ArrayList<>();

            System.out.println("id is : " + id);

            //create effects
            if (temp.get(field) == null) {
                continue;
            } else {
                String[] effects = temp.get(field).split(ParseJSON.PROPEERTY_SEPARATOR); // all string effects here

                for (int i = 0; i < effects.length; i++) {
                    switch (effects[i]) {
                        case "THIRST":
                            effectsList.add(Player.EffectType.THIRST);
                            break;
                        case "DRUNK":
                            effectsList.add(Player.EffectType.DRUNKENNESS);
                            break;
                        case "HUNGRY":
                            effectsList.add(Player.EffectType.HUNGER);
                            break;
                        case "HEALING":
                            effectsList.add(Player.EffectType.HEALING);
                            break;
                        case "INTOXICATED":
                            effectsList.add(Player.EffectType.INTOXICATED);
                            break;
                        case "SMOKY":
                            effectsList.add(Player.EffectType.SMOKY);
                            break;
                        case "TUNE_UP":
                            effectsList.add(Player.EffectType.TUNE_UP);
                            break;
                    }
                }
                map.put(id, effectsList.toArray(new Player.EffectType[]{}));
            }
        }
    }
    //method loads type

    private static void fillTypeHashMap(String field, HashMap<String, Item.Type> map) {
        for (HashMap<String, String> temp : hmArray) {
            String id = temp.get("id");
            Item.Type type;

            String typeString = temp.get(field); // all string effects here

            switch (typeString) {
                case "SUPPLY":
                    type = Item.Type.SUPPLY;
                    break;
                case "BOOK":
                    type = Item.Type.BOOK;
                    break;
                case "CONTAINER":
                    type = Item.Type.CONTAINER;
                    break;
                case "TOOL":
                    type = Item.Type.TOOL;
                    break;
                case "TRASH":
                    type = Item.Type.TRASH;
                    break;
                default:
                    type = Item.Type.OTHER;
                    break;
            }
            map.put(id, type);
        }
    }

    //fill slots
    private static void fillSlotHashMap(String field, HashMap<String, Item.Slot> map) {
        for (HashMap<String, String> temp : hmArray) {
            String id = temp.get("id");
            Item.Slot slotTemp;

            String slotString = temp.get(field); // all string slots here
            if (slotString == null) {
                continue;
            }
            switch (slotString) {
                case "BOOTS":
                    slotTemp = Item.Slot.BOOTS;
                    break;
                case "HELMET":
                    slotTemp = Item.Slot.HELMET;
                    break;
                case "TSHIRT":
                    slotTemp = Item.Slot.TSHIRT;
                    break;
                case "LEGS":
                    slotTemp = Item.Slot.LEGS;
                    break;
                case "JACKET":
                    slotTemp = Item.Slot.JACKET;
                    break;
                case "CLOAK":
                    slotTemp = Item.Slot.CLOAK;
                    break;
                case "GLASSES":
                    slotTemp = Item.Slot.GLASSES;
                    break;
                case "MASK":
                    slotTemp = Item.Slot.MASK;
                    break;
                default:
                    slotTemp = null;
                    break;
            }
            map.put(id, slotTemp);
        }
    }

    public void load(String path) {
        try {
            hmArray.clear();
            hmArray = parseJSONArray(path);

            //fill all data
            fillHashMap("image_id", imageIds);
            fillTypeHashMap("type", types);
            fillHashMap("name", names);
            fillHashMap("description", descriptions);
            fillHashMap("material", materials);
            fillHashMap("property", properties);
            fillHashMap("weight", weights);
            fillHashMap("volume", volumes);
            fillHashMap("power", powers);
            fillHashMap("action", actions);
            fillHashMap("max_durability", maxDurabilities);
            fillHashMap("noise", noises);
            fillHashMap("capacity", capacities);
            fillHashMap("after_use", afterUse);
            fillHashMap("cut_protection", cutProtection);
            fillHashMap("bash_protection", bashProtection);
            fillHashMap("pierce_protection", pierceProtection);
            fillHashMap("comfort", comfort);
            fillSlotHashMap("wear_slot", slot);
            //
            fillEffectHashMap("effect", effect);

            System.out.println(effect);

            ItemPrototype.setPrototype(new ItemPrototype(names, descriptions,
                    maxDurabilities, weights, volumes, noises, materials, types,
                    properties, imageIds, powers, afterUse, effect, cutProtection,
                    bashProtection, pierceProtection, comfort, slot));

        } catch (IOException | NullPointerException ex) {
            System.out.println("Cannot read JSON file, check it.");
            System.out.println(ex.toString());
            System.exit(0);
        }

    }

    public static HashMap<String, String> getNames() {
        return imageIds;
    }
}
